package com.pearadmin.system.service.impl;

import java.util.Date;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.datetime.DateUtil;
import com.pearadmin.common.tools.sequence.SequenceUtil;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.system.domain.SysNotice;
import com.pearadmin.system.service.ISysNoticeService;
import org.springframework.stereotype.Service;
import com.pearadmin.system.mapper.SysAxisMapper;
import com.pearadmin.system.domain.SysAxis;
import com.pearadmin.system.service.ISysAxisService;

import javax.annotation.Resource;

/**
 * 主轴监控Service业务层处理
 * 
 * @author Ryan
 * @date 2021-03-20
 */
@Service
public class SysAxisServiceImpl implements ISysAxisService 
{
    @Resource
    private SysAxisMapper sysAxisMapper;

    @Resource
    private ISysNoticeService sysNoticeService;

    /**
     * 查询主轴监控
     * 
     * @param axisId 主轴监控ID
     * @return 主轴监控
     */
    @Override
    public SysAxis selectSysAxisById(Long axisId)
    {
        return sysAxisMapper.selectSysAxisById(axisId);
    }

    /**
     * 查询主轴的个数
     */
    @Override
    public int selectAxisCount(String axisState){
        return sysAxisMapper.selectAxisCount(axisState);
    }

    @Override
    public List<String> selectAllAxisCount(){

        return sysAxisMapper.selectAllAxisCount();
    }

    /**
     * 查询主轴监控列表
     * 
     * @param sysAxis 主轴监控
     * @return 主轴监控
     */
    @Override
    public List<SysAxis> selectSysAxisList(SysAxis sysAxis)
    {

        return sysAxisMapper.selectSysAxisList(sysAxis);
    }

    /**
     * 查询主轴部分信息
     * @param sysAxis
     * @return 主轴集合
     */
    public List<SysAxis> selectPortionAxis(SysAxis sysAxis){
        return sysAxisMapper.selectPortionAxis(sysAxis);
    }


    /**
     * 查询主轴监控
     * @param sysAxis 主轴监控
     * @param pageDomain
     * @return 主轴监控 分页集合
     * */
    @Override
    public PageInfo<SysAxis> selectSysAxisPage(SysAxis sysAxis, PageDomain pageDomain)
    {
        PageHelper.startPage(pageDomain.getPage(),pageDomain.getLimit());
        List<SysAxis> data = sysAxisMapper.selectSysAxisList(sysAxis);
        SysNotice sysNotice=new SysNotice();
        for (int i=0;i<data.size();i++){
            System.out.println(data.get(i).getAxisState());
            if (data.get(i).getAxisState().equals("故障")){
                if (sysNoticeService.selectSysNoticeBySender(String.valueOf(data.get(i).getAxisId()))){
                    sysNotice.setSender(String.valueOf(data.get(i).getAxisId()));
                    sysNotice.setId(SequenceUtil.makeStringId());
                    sysNotice.setRemark("系统消息");
                    sysNotice.setTitle("主轴故障");
                    sysNotice.setContent("位置为"+data.get(i).getAxisSpace()+",型号为"+data.get(i).getAxisType()+",故障原因:"+data.get(i).getSpare1()+"，请尽快处理。");
                    sysNotice.setType("public");
                    sysNotice.setCreateName(DateUtil.getTime());
                    sysNoticeService.insertSysNotice(sysNotice);
                }
            }
        }
        return new PageInfo<>(data);
    }

    /**
     * 新增主轴监控
     * 
     * @param sysAxis 主轴监控
     * @return 结果
     */

    @Override
    public int insertSysAxis(SysAxis sysAxis)
    {
        System.out.println(sysAxis);
        String axisType=sysAxis.getAxisType();
        System.out.println(sysAxis.getAxisType());
        if (sysAxis.getAxisType().equals("型号1")){
            sysAxis.setAxisPrc("/admin/images/axis_prc1.jpeg");
        }else if (sysAxis.getAxisType().equals("型号2")){
            sysAxis.setAxisPrc("/admin/images/axis_prc2.jpeg");
        }else if (sysAxis.getAxisType().equals("型号3")){
            sysAxis.setAxisPrc("/admin/images/axis_prc3.jpeg");
        }else if (sysAxis.getAxisType().equals("型号4")){
            sysAxis.setAxisPrc("/admin/images/axis_prc4.jpeg");
        }
        sysAxis.setInstallationTime(DateUtil.getNowDate());

        return sysAxisMapper.insertSysAxis(sysAxis);
    }

    /**
     * 修改主轴监控
     * 
     * @param sysAxis 主轴监控
     * @return 结果
     */
    @Override
    public int updateSysAxis(SysAxis sysAxis)
    {
        return sysAxisMapper.updateSysAxis(sysAxis);
    }

    /**
     * 删除主轴监控对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysAxisByIds(String[] ids)
    {
        return sysAxisMapper.deleteSysAxisByIds(ids);
    }

    /**
     * 删除主轴监控信息
     * 
     * @param axisId 主轴监控ID
     * @return 结果
     */
    @Override
    public int deleteSysAxisById(Long axisId)
    {
        return sysAxisMapper.deleteSysAxisById(axisId);
    }

    @Override
    public SysAxis selectAxisInfo(Long axisId){
        return sysAxisMapper.selectAxisInfo(axisId);
    }

}
